package com.jeff.tianti.controller;

import com.alibaba.fastjson.JSON;
import com.jeff.tianti.cms.dto.font.TreeComoTo;
import com.jeff.tianti.cms.entity.Area;
import com.jeff.tianti.cms.service.AreaService;
import com.jeff.tianti.common.ColumnInfoCom;
import com.jeff.tianti.common.dto.AjaxResult;
import com.jeff.tianti.utils.Constants;
import com.jeff.tianti.utils.redis.RedisUtil;
import com.jeff.tianti.utils.redis.ZXWRedisKeyTools;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@CrossOrigin(maxAge = 3600)
@RestController
@RequestMapping("/area")
public class AreaController {
    @Autowired
    private AreaService areaService;
    @Autowired
    private ColumnInfoCom columnInfoCom;

    /**
     * @Ceator:liuYI
     * @Describe:人才天地根据市areaId获取下面所有的区集合
     */
    @RequestMapping(value = "/job/list", method = RequestMethod.GET)
    public AjaxResult findDistrictByAreaId(@RequestParam("areaId") String areaId) {
        HashMap<Object, Object> map = new HashMap<>();
        //获取区集合
        if (StringUtils.isNotBlank(areaId)) {
            List<Area> areas = areaService.findDistrictByAreaId(areaId);
            map.put("area", areas);
        }
        //获取职务列表
        Map cmsMap = columnInfoCom.findColumnInfoList(Constants.JOB_CATEGORY);
        map.put("map", cmsMap);
        return AjaxResult.setSuccessData(map);
    }

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * @Ceator:liuYI
     * @Describe:获取地区树形结构
     */
    @RequestMapping(value = "/getTree", method = RequestMethod.GET)
    public AjaxResult getTree() {
        RedisUtil redisUtil = new RedisUtil();
        redisUtil.setRedisTemplate(redisTemplate);
        String o;
        try {
            o = (String) redisUtil.get(ZXWRedisKeyTools.AREA_TREE.getPrefix());
        } catch (Exception e) {
            o = null;
            e.printStackTrace();
        }
        List<TreeComoTo> treeComoTos = null;
        if (StringUtils.isNotBlank(o)) {//不为空
            treeComoTos = JSON.parseArray(o, TreeComoTo.class);
        } else {
            Byte b = new Byte("1");
            List<Area> areas = areaService.queryAreaList(null);
            List<Area> all = areas.stream().filter(t -> b.equals(t.getLevel())).collect(Collectors.toList());
            List<TreeComoTo> sheng = new LinkedList<>();//省
            all.stream().forEach(t -> {
                TreeComoTo treeComoTo = new TreeComoTo();
                treeComoTo.setValue(t.getId());
                treeComoTo.setLabel(t.getName());
                sheng.add(treeComoTo);
            });
            sheng.stream().forEach(t -> {
                List<TreeComoTo> shi = findChildrenByParent(t, areas);//市
                shi.stream().forEach(t0 -> {
                    List<TreeComoTo> qu = findChildrenByParent(t0, areas);//区
                    t0.setChildren(qu);
                });
                t.setChildren(shi);
            });
            redisUtil.set(ZXWRedisKeyTools.AREA_TREE.getPrefix(), JSON.toJSONString(sheng));
            treeComoTos = sheng;
        }
        return AjaxResult.setSuccessData(treeComoTos);
    }


    public List<TreeComoTo> findChildrenByParent(TreeComoTo comoTo, List<Area> all) {
        List<Area> collect = all.stream().filter(t -> t.getParentAreaId().equals(comoTo.getValue())).collect(Collectors.toList());
        List<TreeComoTo> list = new LinkedList<>();
        collect.forEach(t -> {
            TreeComoTo treeComoTo = new TreeComoTo();
            treeComoTo.setValue(t.getId());
            treeComoTo.setLabel(t.getName());
            list.add(treeComoTo);
        });
        return list;
    }

    @RequestMapping(value = "/getProvince", method = RequestMethod.GET)
    public List<Area> getProvince() {
        return areaService.getByLevel(Byte.parseByte("1"));
    }
}
